package com.itzb.asone.fileserver.impl;


import java.util.Date;

import com.itzb.asone.fileserver.IFileServer;
import com.itzb.asone.fileserver.channel.FileServerConstant;
import com.itzb.asone.fileserver.channel.IChannelService;
import com.itzb.asone.fileserver.channel.qcloud.QcloudChannelService;
import com.itzb.asone.fileserver.model.FSResult;
import com.itzb.asone.mybatis.Criteria;
import com.itzb.asone.system.model.SysUploadFile;
import com.itzb.asone.system.service.IMessageService;
import com.itzb.asone.system.service.ISysParamService;
import com.itzb.asone.system.service.ISysUploadFileService;
import com.itzb.asone.utils.GitId;

public class FileServer implements IFileServer{
	
	private IMessageService messageService;
	private ISysParamService sysParamService;
	private ISysUploadFileService sysUploadFileService;
	private IChannelService channelService ;
	
	public void init(){
		
		String FILE_SERVER = sysParamService.getParamValue("FILE_SERVER");
		
		if(FILE_SERVER.equals(FileServerConstant.FILE_SERVER_QCLOUD)){
			channelService = new QcloudChannelService();
		} else if(FILE_SERVER.equals(FileServerConstant.FILE_SERVER_LOCAL)){
			
		}
		
	}
	

	
	public FSResult getFileList(String path) {
		init();
		return channelService.getFileList(path);
	}

	
	public FSResult deleteFolder(String path) {
		init();
		return channelService.deleteFolder(path);
	}

	
	public FSResult uploadFile(String filePath, String fileName) {
		init();
		FSResult fsResult = channelService.uploadFile(filePath, fileName);
		SysUploadFile sysUploadFile = new SysUploadFile();
		sysUploadFile.setId(GitId.primaryKey());
		sysUploadFile.setFileUrl(fsResult.getList().get(0).getFilePath());
		sysUploadFile.setServiceType(fsResult.getList().get(0).getFileUrl());
		sysUploadFile.setFileStatus(1);
		sysUploadFile.setCreateTime(new Date());
		sysUploadFileService.insert(sysUploadFile);
		fsResult.setId(sysUploadFile.getId());
		return fsResult;
	}

	
	public FSResult deleteFile(String filePath) {
		init();
		Criteria<SysUploadFile> param = new Criteria<SysUploadFile>();
		param.addParam("fileUrl", filePath);
		SysUploadFile sysUploadFile = sysUploadFileService.queryPage(param).get(0);
		FSResult fsResult = channelService.deleteFile(sysUploadFile.getServiceType());
		sysUploadFile.setFileStatus(1);
		param.setRecord(sysUploadFile);
		sysUploadFileService.updateByCriteria(param);
		return fsResult;
	}


	public IMessageService getMessageService() {
		return messageService;
	}


	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}


	public ISysParamService getSysParamService() {
		return sysParamService;
	}

	public void setSysParamService(ISysParamService sysParamService) {
		this.sysParamService = sysParamService;
	}

	public ISysUploadFileService getSysUploadFileService() {
		return sysUploadFileService;
	}

	public void setSysUploadFileService(ISysUploadFileService sysUploadFileService) {
		this.sysUploadFileService = sysUploadFileService;
	}
	
	
}
